package com.cecsmsserve.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cecsmsserve.entity.NewsCategory;
import com.cecsmsserve.entity.NewsCategoryRel;
import com.cecsmsserve.mapper.NewsCategoryMapper;
import com.cecsmsserve.mapper.NewsCategoryRelMapper;
import com.cecsmsserve.service.NewsCategoryService;
import com.cecsmsserve.util.result.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class NewsCategoryServiceImpl extends ServiceImpl<NewsCategoryMapper, NewsCategory> implements NewsCategoryService {

    @Autowired
    private NewsCategoryMapper categoryMapper;
    
    @Autowired
    private NewsCategoryRelMapper categoryRelMapper;

    @Override
    public CommonResult addCategory(NewsCategory category) {
        // 设置默认排序和状态
        if (category.getSortOrder() == null) {
            category.setSortOrder(0);
        }
        if (category.getStatus() == null) {
            category.setStatus(1);
        }
        
        boolean saved = save(category);
        if (!saved) {
            return CommonResult.failed("添加分类失败");
        }
        
        return CommonResult.success(category.getId());
    }

    @Override
    public CommonResult updateCategory(NewsCategory category) {
        NewsCategory existCategory = getById(category.getId());
        if (existCategory == null) {
            return CommonResult.failed("分类不存在");
        }
        
        boolean updated = updateById(category);
        if (!updated) {
            return CommonResult.failed("更新分类失败");
        }
        
        return CommonResult.success(null);
    }

    @Override
    public CommonResult deleteCategory(Integer id) {
        // 检查分类是否被使用
        LambdaQueryWrapper<NewsCategoryRel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NewsCategoryRel::getCategoryId, id);
        long count = categoryRelMapper.selectCount(wrapper);
        
        if (count > 0) {
            return CommonResult.failed("该分类已被使用，无法删除");
        }
        
        boolean removed = removeById(id);
        if (!removed) {
            return CommonResult.failed("删除分类失败");
        }
        
        return CommonResult.success(null);
    }

    @Override
    public CommonResult getCategoryList() {
        LambdaQueryWrapper<NewsCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(NewsCategory::getSortOrder);
        
        List<NewsCategory> categories = list(wrapper);
        return CommonResult.success(categories);
    }

    @Override
    public CommonResult updateCategoryStatus(Integer id, Integer status) {
        NewsCategory category = new NewsCategory();
        category.setId(id);
        category.setStatus(status);
        
        boolean updated = updateById(category);
        if (!updated) {
            return CommonResult.failed("更新分类状态失败");
        }
        
        return CommonResult.success(null);
    }

    @Override
    public CommonResult getNewsCategoryIds(Integer newsId) {
        LambdaQueryWrapper<NewsCategoryRel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NewsCategoryRel::getNewsId, newsId);
        
        List<NewsCategoryRel> rels = categoryRelMapper.selectList(wrapper);
        List<Integer> categoryIds = rels.stream()
                .map(NewsCategoryRel::getCategoryId)
                .collect(Collectors.toList());
        
        return CommonResult.success(categoryIds);
    }
} 